package _8nio;

import org.junit.Test;
import sun.nio.ch.DirectBuffer;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: wang zhen
 * @create: 2022-08-04 09:57
 */
public class DirectMemoryTest {

    public static void main(String[] args) throws IOException {

        FileChannel fileChannel=FileChannel.open(Paths.get("C:\\Users\\test\\Desktop\\aaa.jpeg"), StandardOpenOption.READ);
        MappedByteBuffer map = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());

        //
        if(map.isDirect()){
            DirectBuffer d=(DirectBuffer) map;
            d.cleaner().clean();//执行map的线程，进行回收
        }

        fileChannel.close();
    }

    @Test
    public void gcTest1() throws InterruptedException {

        List<ByteBuffer> list=new ArrayList<>();
        while (true){
            Thread.sleep(200);
            ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 1024);
            //list.add(buffer);
        }

    }
}
